const chalk = require('chalk')
const glob = require('glob')
const path = require('path')

const pages = glob.sync('./src/pages/**/main.js').reduce((pages, path) => {
  const chunk = path.split('./src/pages/')[1].split('/main.js')[0]
  pages[chunk] = {
    entry: `src/pages/${chunk}/main.js`,
    template: `src/pages/${chunk}/main.html`,
    filename: `${chunk}.html`,
  }
  return pages
}, {})

console.log(chalk.green(`编译 pages(${Object.values(pages).length}):\n${Object.values(pages).map(page => page.filename).join('\\n')}\n`))

function resolve (dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  pages,
  lintOnSave: false,
  baseUrl: '',
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@$', resolve('src'))
      .set('assets', resolve('src/assets'))
      .set('components', resolve('src/components'))
  },
  css: {
    loaderOptions: {
      sass: {
        data: `@import "@/style/variables.scss";`
      }
    }
  }
}
